package Operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{

    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入书名：");
        String name = scanner.nextLine();
        System.out.print("请输入作者：");
        String author = scanner.nextLine();
        System.out.print("请输入书的类型：");
        String type = scanner.nextLine();
        System.out.print("请输入价格：");
        int price = scanner.nextInt();

        int current = bookList.getUsedSize();
        //先检查 存放书籍的位置 是否已满
        if(current == bookList.getBooks().length) {
            System.out.println("书架已满");
            return;
        }

        //接下来检查 将新增的书籍 是否已存在
        for (int i = 0; i < current; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("该书籍已存在");
                return;
            }
        }

        //新增书籍
        bookList.getBooks()[current] = new Book(name,author,price,type);
        bookList.setUsedSize(current+1);
        System.out.println("新增成功");
    }

}
